package com.example.a2032.activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a2032.R;
import com.example.a2032.Utils.ClockUtils;
import com.example.a2032.Utils.Utils;
import com.example.a2032.entity.JstyleClock;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ClockNotifyActivity extends BaseActivity {

    @BindView(R.id.iv_clockType)
    ImageView ivClockType;
    @BindView(R.id.tv_notifyName)
    TextView tvNotifyName;
    @BindArray(R.array.clockName_array)
    String[] clockNameArray;
    public static final String KEY_ClockType = "ClockType";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify);
        ButterKnife.bind(this);
        Utils.wakeUpAndUnlock(this);
        init();
    }

    private static final String TAG = "ClockNotifyActivity";
    MediaPlayer mediaPlayer;
    private void init() {
        Utils.wakeUpAndUnlock(this);
        Utils.startVibrato(this, 2000);
        int clockType = getIntent().getIntExtra(KEY_ClockType, JstyleClock.CLOCK_NORMAL);
        Log.i(TAG, "init: " + clockType);
        String ringName= getClockRingName(clockType);
        mediaPlayer=Utils.startPlayAssetRing(this,ringName);
        Utils.startMeasureTimer(1, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                disposable = d;
            }

            @Override
            public void onNext(Object o) {
                cancelPlay();
                finish();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

        showNotifyInfo();
    }

    String getClockRingName(int clockType) {
        String name="";
        switch (clockType) {
            case JstyleClock.CLOCK_NORMAL:
                name="ring_normal.mp3";
                break;
            case JstyleClock.CLOCK_DRINK:
                name="ring_drink.mp3";
                break;
            case JstyleClock.CLOCK_FOOD:
                name="ring_food.mp3";
                break;
            case JstyleClock.CLOCK_MEDICA:
                name="ring_medical.mp3";
                break;
        }
        return name;
    }
    boolean isCancel;
    //定时结束跟finish的时候都会走一遍
    void cancelPlay() {
        //vibrator.cancel();
        if(!isCancel&&mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
            isCancel=true;
        }


    }
    /**
     * 普通闹钟则显示时间，其他显示闹钟种类名称
     *
     * @param
     */
    void showNotifyInfo() {
        JstyleClock jstyleClock = (JstyleClock) getIntent().getSerializableExtra(ClockUtils.JSTYLE_CLOCK);
        int clockType = jstyleClock.getClockType();
        String clockTime = Utils.getFormatTimeString(jstyleClock.getHour(), jstyleClock.getMinute());
        int[] resIds = Utils.getResourcesImageArray(this, R.array.clock_icon_array);
        String notifyName = clockType == JstyleClock.CLOCK_NORMAL ? clockTime : clockNameArray[clockType];
        tvNotifyName.setText(notifyName);
        ivClockType.setImageResource(resIds[clockType]);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelPlay();
    }
}